package com.group.yqxxb.controller;

import com.alibaba.fastjson.JSONObject;
import com.group.yqxxb.response.R;
import com.group.yqxxb.service.FriendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.swing.text.StyledEditorKit;

@RestController
@RequestMapping("/friend")
public class FriendController {

    @Autowired
    FriendService friendService;

    /**
     * 添加好友
     * @param accid
     * @param faccid
     * @param type
     * @param msg
     * @return
     */
    @PostMapping("add")
    public R addFriend(@RequestParam("accid") String accid,
                       @RequestParam("faccid") String faccid,
                       @RequestParam("type") Integer type,
                       @RequestParam(value = "msg",required = false) String msg){
        try{
            friendService.AddFriend(accid, faccid, type, msg);
        }catch (Exception e){
            return R.error(e);
        }
        if (type == 1){
            return R.success("直接加好友成功");
        }else if(type == 2){
            return R.success("请求加好友成功");
        }else if (type == 3){
            return R.success("同意加好友");
        }else {
            return R.success("拒绝加好友");
        }
    }

    /**
     * 更新好友信息
     * @param accid
     * @param faccid
     * @param alias
     * @param ex
     * @return
     */
    @PostMapping("update")
    public R updateFriend (@RequestParam("accid") String accid,
                           @RequestParam("faccid") String faccid,
                           @RequestParam(value = "alias",required = false) String alias,
                           @RequestParam(value = "ex",required = false) String ex)
    {
        try{
            friendService.UpdateFriend(accid, faccid, alias, ex);
        }catch (Exception e){
            return R.error(e);
        }
        return R.success("更新好友信息成功");
    }

    //删除好友
    @PostMapping("delete")
    public R updateFriend (@RequestParam("accid") String accid,
                           @RequestParam("faccid") String faccid,
                           @RequestParam(value = "isDeleteAlias",required = false) Boolean isDeleteAlias)
    {
        try{
            friendService.DeleteFriend(accid, faccid, isDeleteAlias);
        }catch (Exception e){
            return R.error(e);
        }
        return R.success("删除好友成功");
    }

    //获取好友关系
    @PostMapping("get")
    public R getFriend(@RequestParam("accid") String accid) {
        JSONObject jsonObject;
        try {
            jsonObject = friendService.GetFriend(accid);
        } catch (Exception e) {
            return R.error(e);
        }
        return R.success(jsonObject);
    }

    //设置黑名单或静音
    @PostMapping("setSpecialRelation")
    public R setSpecialRelation(@RequestParam("accid") String accid,
                                @RequestParam("targetAcc") String targetAcc,
                                @RequestParam("relationType") Integer relationType,
                                @RequestParam("value") Integer value){
        try{
            friendService.SetSpecialRelation(accid, targetAcc, relationType, value);
        }catch (Exception e){
            return R.error(e);
        }
        return R.success("设置黑名单/静音成功");
    }




}
